<?php
jimport('joomla.application.component.model');
class svModelPhongban extends JModel
{
	//------------ tim kiem----------------------------------
	function getAllDonviquanly()//ok
	{
		$query='SELECT dvql_id,dvql_ten FROM qlcb_donviquanly WHERE 1';
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getAllPhongban()
	{
		$query='SELECT pb.*,dvql.dvql_ten
			    FROM qlcb_donviquanly as dvql,qlcb_phongban as pb
			    WHERE dvql.dvql_id=pb_iddonviquanly';
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getPhongbanByID($id)
	{
		$query='SELECT pb.*,dvql.dvql_ten
		FROM qlcb_donviquanly as dvql,qlcb_phongban as pb
		WHERE dvql.dvql_id=pb_iddonviquanly AND pb_id='.$id;
		$result=$this->_loadObject($query);
		return $result;
	}
	//----------------------------them ,sua ,xoa -------------------------------
	function addNewPhongban($iddonviquanly,$ten,$diachi,$dienthoai,$email,$mieuta)
	{
		$query="INSERT INTO qlcb_phongban(pb_iddonviquanly,pb_ten,pb_diachi,pb_dienthoai,pb_email,pb_mieuta)
		 VALUES('".$iddonviquanly."','".$ten."','".$diachi."','".$dienthoai."','".$email."','".$mieuta."')";
		$message='Thêm mới ';
		$result=$this->_Execute($query, $message);		
		return $result.' <b style="font-size:18px;color:blue;">'.$ten.'</b>';
	}
	// sua
	function editPhongban($idphongban,$iddonviquanly,$ten,$diachi,$dienthoai,$email,$mieuta)
	{
		$query="UPDATE qlcb_phongban SET pb_iddonviquanly='".$iddonviquanly."',pb_ten='".$ten."'
											,pb_diachi='".$diachi."',pb_dienthoai='".$dienthoai."',pb_email='".$email."',
											pb_mieuta='".$mieuta."'
	   		    WHERE pb_id=".$idphongban;
		$message='Chỉnh sửa ';
		$result=$this->_Execute($query, $message);
		return $result.' <b style="font-size:18px;color:blue;">'.$ten.'</b>';
	}
	function delPhongban($idphongban)
	{
		$query="DELETE FROM qlcb_phongban WHERE pb_id=".$idphongban;
		$message='Xóa 1 item ';
		$result=$this->_Execute($query, $message);
		return '<b style="font-size:18px;color:blue;">'.$result.'</b>';	
	}
	// xoa
	function delDonvi($id)
	{
		$query="DELETE FROM qlcb_donviquanly WHERE dvql_id=".$id;
		$message='Xóa ';
		$result=$this->_Execute($query, $message);
		return $result.' một item';
	}
	// cac ham phuc vu truy van CSDL
	function _loadObject($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObject();
		return $result;
	}
	function _loadObjectList($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObjectList();
		return $result;
	}
	function _Execute($query,$message)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		try {
			$db->query();
			return $message." thành công ";
		}
		catch (Exception $e){
			return $message." Không thành công! Vui lòng thử lại.";
		}
	}
}
?>